import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

public class Main {


    private static final ArrayList<Process> PROCESSES = new ArrayList<>();

    public static void main(String[] args) {
        String os = System.getenv("OS");
        printMsg("当前操作系统：" + os);

        if (!os.toLowerCase().startsWith("windows")) {
            printMsg("请使用Windows系统运行本程序！");
            return;
        }
        Scanner scanner = new Scanner(System.in);
        Runtime runtime = Runtime.getRuntime();
        exit(inputSourceFilesDir(scanner, runtime));
    }

    /**
     * 退出程序
     */
    private static void exit(int code) {
        for (Process process : PROCESSES) {
            if (process.isAlive()) {
                process.destroy();
            }
        }
        printMsg("程序已经退出,结果码：" + code);
    }

    /**
     * 输入原始TS文件夹的路径
     *
     * @param scanner 文本输入接收
     * @param runtime runtime环境
     * @return
     */
    private static int inputSourceFilesDir(Scanner scanner, Runtime runtime) {
        printMsg("请输入原始TS文件夹的路径");
        File sourceDirFile;
        while (true) {
            boolean nextLine = scanner.hasNextLine();
            if (!nextLine) {
                continue;
            }
            String s = scanner.nextLine().trim();
            if (s.isEmpty()) {
                printMsg("请输入原始TS文件夹的路径(输入exit或quit退出程序)：");
                continue;
            }

            if (s.equalsIgnoreCase("exit") || s.equalsIgnoreCase("quit")) {
                return ConstantsCode.EXIT;
            }
            printMsg("输入的目录：" + s);
            sourceDirFile = new File(s);
            if (!sourceDirFile.exists()) {
                printMsg("该目录不存在，请重新输入原始TS文件夹的路径(输入exit或quit退出程序)：");
                continue;
            }
            if (!sourceDirFile.isDirectory()) {
                printMsg("不是文件夹，请重新输入原始TS文件夹的路径(输入exit或quit退出程序)：");
                continue;
            }
            break;
        }

        return inputTargetDir(scanner, runtime, sourceDirFile);
    }

    /**
     * @param scanner
     * @param runtime
     * @param sourceDirFile
     * @return
     */
    private static int inputTargetDir(Scanner scanner, Runtime runtime, File sourceDirFile) {
        printMsg("请输入合并后的文件存放的文件夹");
        File targetDirFile;
        while (true) {
            boolean nextLine = scanner.hasNextLine();
            if (!nextLine) {
                continue;
            }
            String s = scanner.nextLine().trim();
            if (s.isEmpty()) {
                printMsg("请输入原始TS文件夹的路径(输入exit或quit退出程序)：");
                continue;
            }

            if (s.equalsIgnoreCase("exit") || s.equalsIgnoreCase("quit")) {
                return ConstantsCode.EXIT;
            }
            printMsg("输入的目录：" + s);
            targetDirFile = new File(s);
            if (!targetDirFile.exists()) {
                printMsg("该目录不存在，请重新输入合并后的文件存放的文件夹(输入exit或quit退出程序)：");
                continue;
            }
            if (!targetDirFile.isDirectory()) {
                printMsg("不是文件夹，请重新输入合并后的文件存放的文件夹(输入exit或quit退出程序)：");
                continue;
            }
            break;
        }
        String targetFilePath = generateTargetFileName(targetDirFile);
        String cmd = "cmd /c copy /b " + sourceDirFile.getAbsolutePath() + "\\*.ts " + targetFilePath;
        printMsg("执行指令：" + cmd);
        try {
            Process process = runtime.exec(cmd);
            PROCESSES.add(process);
            InputStream inputStream = process.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            while (process.isAlive()) {
                String s = bufferedReader.readLine();
                if (s != null) {
                    printMsg(s);
                }
            }
            printMsg("合并完成！");
        } catch (IOException e) {
            e.printStackTrace();
            return ConstantsCode.ERROR;
        }
        return ConstantsCode.SUCCESS;
    }

    private static String generateTargetFileName(File targetDirFile) {
        boolean needBreak = false;
        File targetFile = new File(targetDirFile + "\\merged.ts");
        ;
        int count = 0;
        while (!needBreak) {
            if (!targetFile.exists()) {
                needBreak = true;
                continue;
            }
            count++;
            targetFile = new File(targetDirFile + "\\merged" + count + ".ts");
        }
        return targetFile.getAbsolutePath();
    }

    private static void printMsg(String msg) {
        System.out.println(msg);
    }
}